﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SLBehaviourSystem
{
    [ActionNode]
    public class ActionNode: BehaviourNodeBase, IActionNode
    {
        public override BehaviourNodeState Excute()
        {
            base.Excute();
            Component.BindNode(this);
            switch (ExcuteType)
            {
                case BehaviourExcuteType.Sync:
                    curNodeState = Component.Excute();
                    if (curNodeState == BehaviourNodeState.Success || curNodeState == BehaviourNodeState.Failure)
                    {
                        if (parent != null)
                        {
                            parent.ChildNodeComplete();
                        }
                    }
                    break;
                case BehaviourExcuteType.Async:

                    curNodeState = BehaviourNodeState.Running;
                    Component.ExcuteAsync((nodeState)=> 
                    {
                        curNodeState = nodeState;
                        if (curNodeState == BehaviourNodeState.Success || curNodeState == BehaviourNodeState.Failure)
                        {
                            if (parent != null)
                            {
                                parent.ChildNodeComplete();
                            }
                        }
                    });
                    break;
                default:
                    break;
            }

            return curNodeState;
        }

        protected override void OnReset()
        {
            base.OnReset();
            Component.Reset();
        }
    }
}
